///////////////////////////////////////////////////////////////////////////////
// Project: Hoyo Client Game
//
// game_server.cpp
// Created: 2013-12-26
// Author: Like Cheng
///////////////////////////////////////////////////////////////////////////////
#include "game_server.h"
#include "hy_message_dispersed_center.h"

#include "hy_db/hy_db_logic.h"

#include "hy_battle_system/hy_monster_db_logic.h"
//#include "hy_battle_system/hy_copy_logic/hy_copy_db_logic.h"
#include "hy_player_system/hy_create_role_logic.h"
#include "hy_common_system/hy_language_logic.h"
#include "hy_common_system/hy_random.h"
#include "hy_battle_system/hy_skill_db_logic.h"

namespace hoyosvr
{
	hoyosvr::game_server::ptr game_server::get_inst()
	{
		static ptr p(new game_server());
		return p;
	}

	void game_server::on_init()
	{
		//_server_handler_ptr = server_handler::ptr(new server_handler);
		
		HINFO("game_server init...");

		HINFO( "game database init..." );
		hy_db_logic::get_instance()->init();

		HINFO( "network message protocal center...");
		hy_message_dispersed_center::instance()->init_message();

		HINFO( "~~load server data begin~~");
		load_server_data();
		HINFO( "~~load server data end~~");	
	}

	void game_server::on_update()
	{
		int t = int(time(NULL));
		if ( t == _updateTime )
		{
 			return;
		}
		_updateTime = t;
	}

	void game_server::on_stop()
	{

	}

	void game_server::on_run()
	{
		
	}

	void game_server::client_connect_handler( tcp_session_ptr session,int error_value )
	{

	}

	void game_server::sync_process_recv_msg( int net_id, hy_network_message::ptr recv_msg_ptr )
	{
		//client_map::iterator it = _map_client.find(net_id);
		//if ( it != _map_client.end() )
		//{
			recv_msg_ptr->set_net_id( net_id );
			hy_message_dispersed_center::instance()->message_dispersed( recv_msg_ptr.get() );
		//}
	}

	bool game_server::async_process_recv_msg( tcp_session_ptr conn, hy_network_message::ptr recv_msg_ptr )
	{
		return false;
	}

	void game_server::async_send_to_client( int net_id, hy_network_message& m )
	{
		client_map::iterator it = _map_client.find(net_id);
		if ( it != _map_client.end() )
		{
			std::string t = m.get_string();
			hoyosvr::msg::hoyo_msg::ptr hm = hoyosvr::msg::hoyo_msg::create( t.c_str(), t.length() );
			it->second->write_hoyo_msg( *hm );
		}
	}
	
	void game_server::load_server_data()
	{
		
 		HINFO( "-load monster_template data begin-");	
 		if( hy_monster_db_logic::instance()->load_monster_template_data() )
 			HINFO( "-load monster_template data end--");	
 		else
 			HERROR( "-load monster_template data failed!--");	

 		HINFO( "-load monster_group_template data begin-");	
 		if( hy_monster_db_logic::instance()->load_monster_group_template_data() )
 			HINFO( "-load monster_group_template data end--");	
 		else
 			HERROR( "-load monster_group_template data failed!--");
// 
// 		HINFO( "-load copy data begin-");	
// 		if( hy_copy_db_logic::instance()->load_copy_data() )
// 			HINFO( "-load copy data end--");	
//		else
//			HERROR( "-load copy data failed!--");

		HINFO( "-load random name data begin-");	
		if( hy_create_role_logic::instance()->init_load_db() )
			HINFO( "-load random name data end--");	
		else
			HERROR( "-load random name data failed!--");

		HINFO( "-load language data begin-");	
		if( hy_language_logic::instance()->init() )
			HINFO( "-load language data end--");	
		else
			HERROR( "-load language data failed!--");
	
 		HINFO( "-load server skill data begin-");	
 		if( hy_skill_db_logic::instance()->load_server_skill_data() )
 			HINFO( "-load server skill data end--");	
 		else
 			HERROR( "-load server skill data failed!--");
		
	}

	void game_server::reload_server_data()
	{

		HINFO( "-reload monster_template data begin-");	
		if( hy_monster_db_logic::instance()->reload_monster_template_data() )
			HINFO( "-reload monster_template data end--");	

		HINFO( "-reload server skill data begin-");	
		if( hy_skill_db_logic::instance()->reload_server_skill_data() )
			HINFO( "-reload server skill data end--");	
		else
			HERROR( "-reload server skill data failed!--");
	}
	
	void game_server::release_instance()
	{
		hy_monster_db_logic::purge_instance();
		hy_message_dispersed_center::purge_instance();
		hy_language_logic::purge_instance();
		/*
		hy_skill_db_logic::purge_instance();
		hy_create_role_logic::purge_instance();
		hy_copy_db_logic::purge_instance();
		*/
	}
}